//
//  PingTeViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/26.
//

#import "PingTeViewController.h"
#import "PingTeHeaderView.h"
#import "PingTeTableViewCell.h"
#import "CommonUtil.h"
#import "DrawPrizeModel.h"

#import "PingTeBottomView.h"
#import "GangcaiChangeAlert.h"

@interface PingTeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) PingTeHeaderView * headerView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray * models;

@property (strong, nonatomic) PingTeBottomView * bottomView;


@end

@implementation PingTeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.view.backgroundColor = RGB(243.0, 243.0, 243.0);
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"港彩名人堂" forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"icon_mingrentang_pull"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, AD(100), AD(44));
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(195));
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, AD(10));
    btn.width = AD(200);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = btn;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(rightBtnClick:)];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self testData];
}



// 假数据
- (void)testData{
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        DrawPrizeModel *model = [[DrawPrizeModel alloc] init];
        if (i == 0) {
            model.showBtn = YES;
        }
        
        
        [self.models addObject:model];
    }
    [self.tableView reloadData];
}

- (void)btnClick:(UIButton *)sender{
    GangcaiChangeAlert *alert = (GangcaiChangeAlert *)[HudFactory creatHudWithType:@"GangcaiChangeAlert"];
    alert.currentTitle = sender.titleLabel.text;
    alert.actionBlock = ^(NSString *data) {
        NSLog(@"%@",data);
        [sender setTitle:data forState:UIControlStateNormal];
    };
    [alert show];
}


- (void)rightBtnClick:(UIButton *)sender{
    NSLog(@"分享");
}





#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PingTeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.models.count > indexPath.row) {
       cell.prizeModel = self.models[indexPath.row];
    }
   // cell.superVC = self;
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   // [self.navigationController pushViewController:[ReplayDetailViewController new] animated:YES];


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.models.count > indexPath.row) {
        DrawPrizeModel *model = self.models[indexPath.row];
        if (model.showBtn) {
            return AD(135);
        }
    }
    return AD(95);
}




-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kNavBarAndStatusBarHeight)];
        [_tableView registerClass:[PingTeTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, kTabBarHeight, 0);
        _tableView.backgroundColor = [UIColor clearColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(PingTeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[PingTeHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(0))];
        __weak typeof(self) weakSelf = self;
        _headerView.actionBlock = ^(NSString * _Nonnull title) {
            if ([title isEqualToString:@"特码"]) {
                weakSelf.models = nil;
            }else{
                [weakSelf testData];
            }
            [weakSelf.tableView reloadData];
            [CommonUtil setupPlaceholderView:weakSelf.tableView models:weakSelf.models];
        };

    }
    return _headerView;
}


-(PingTeBottomView *)bottomView{
    if (!_bottomView) {
        CGFloat h = kTabBarHeight + AD(20);
        _bottomView = [[PingTeBottomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-h, SCREEN_WIDTH, h)];
    }
    return _bottomView;
}

@end
